package com.bdqn.zhao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bdqn.zhao.dao.IpParkMapper;
import com.bdqn.zhao.dao.SysDeptMapper;
import com.bdqn.zhao.dao.SysRoleMapper;
import com.bdqn.zhao.dao.SysUserMapper;
import com.bdqn.zhao.pojo.*;
import com.bdqn.zhao.service.SysUserService;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

/**
 * <p>
 * 用户信息表 服务实现类
 * </p>
 *
 * @author zhangsan
 * @since 2023-04-24
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
   @Autowired
   public SysRoleMapper sysRoleMapper;
    @Autowired
    public IpParkMapper ipParkMapper;
    @Autowired
    public SysUserMapper sysUserMapper;
    @Autowired
    public SysDeptMapper sysDeptMapper;
    @Override
    public List<SysUser> selectAllJ() {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        IPage<SysUser> sysUserIPage=new Page<>(0,10);
        IPage<SysUser> sysUserIPage1 = sysUserMapper.selectPage(sysUserIPage, queryWrapper);
        List<SysUser> records = sysUserIPage1.getRecords();
        return records;

    }

    @Override
    public SysUser selectbyidJ(int id) {
        MPJLambdaWrapper<SysUser> wrapper = new MPJLambdaWrapper<SysUser>()
                .selectAll(SysUser.class) //查询 ip_room 房间表全部字段;
                .leftJoin(SysDept.class, SysDept::getDeptId,SysUser::getDeptId)
                .selectAs(SysDept::getDeptName,SysUser::getDeptName)
                .leftJoin(SysRole.class,SysRole::getRoleId,SysUser::getUserType)
                .selectAs(SysRole::getRoleName,SysUser::getRoleName)
                .leftJoin(IpPark.class,IpPark::getParkId,SysUser::getParkId)
                .selectAs(IpPark::getParkName,SysUser::getParkName)
                .eq("t.user_id",id)
                ;
        SysUser sysUser = sysUserMapper.selectOne(wrapper);

        return
                sysUser;
    }

    @Override
    public int delectSysuer(int id) {
        return sysUserMapper.deleteById(id);
    }

    @Override
    public int updatePwd(SysUser sysUser) {
        return sysUserMapper.updatePwd(sysUser);
    }

    @Override
    public int sysuserAdd(SysUser sysUser) {
        SysUser sysUser1=new SysUser();
        sysUser1.setUserName(sysUser.getUserName());
        sysUser1.setLoginName(sysUser.getLoginName());
        sysUser1.setMobile(sysUser1.getMobile());
        sysUser1.setStatus(sysUser1.getStatus());
        return sysUserMapper.insert(sysUser);
    }

    @Override
        public List<SysUser> fuzzy(@PathVariable Integer pageNum,
                               @PathVariable Integer pageSize,String status, String userName) {
        QueryWrapper<SysUser> queryWrapper=new QueryWrapper<>();
        if (pageNum <= 0) {
            pageNum = 1;
        }
        Page<SysUser> page = new Page<>(pageNum, pageSize);//设置分页参数
        if (status!=null){
            queryWrapper.eq("status",status);
        } else if (status!=null&&userName!=null) {
            queryWrapper.eq("status",status).eq("userName",userName);
        }
        IPage<SysUser> sysUserIPage = sysUserMapper.selectPage(page, queryWrapper);
        List<SysUser> sysUsers;
        sysUsers=sysUserIPage.getRecords();
        return sysUsers;
    }

    @Override
    public List<SysDept> department() {
        QueryWrapper<SysDept> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("dept_id","dept_name");
        return sysDeptMapper.selectList(queryWrapper);
    }

    @Override
    public List<IpPark> departmentyq() {
        return ipParkMapper.selectList(new QueryWrapper<IpPark>().select("park_id","park_name"));
    }

    @Override
    public List<SysRole> departmentjs() {
        return sysRoleMapper.selectList(new QueryWrapper<SysRole>().select("role_id","role_name"));
    }

    @Override
    public int updatestaus(String loginName,String status) {
        return sysUserMapper.updatestatus(status,loginName);
    }
}
